package com.jueming.miser.auth.controller;

import com.jueming.miser.auth.feign.OrderFeignService;
import com.jueming.miser.auth.service.CartService;
import com.jueming.miser.auth.to.MiserOrderTo;
import com.jueming.miser.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("auth/misercart")
public class CartController {

    @Autowired
    OrderFeignService orderFeignService;

    @Autowired
    CartService cartService;

    @RequestMapping("/addtocart")
    public R addToCart(@RequestParam("wareId") Integer wareId,
                       @RequestParam("wareCount") Integer wareCount) {
        return cartService.addToCart(wareId, wareCount);
    }

    @RequestMapping("/list")
    public R list() {
        return R.ok().setData(cartService.listCart());
    }

    @RequestMapping("/postcart")
    public R postcart() {
        List<MiserOrderTo> tos = cartService.listCart();
        for (MiserOrderTo to : tos) {
            orderFeignService.saveto(to);
        }
        //TODO：订单合法性检查
        return R.ok("提交订单成功");
    }

}
